//
//  CarBuyingDetailsViewController.m
//  YaDiuDiu
//
//  Created by 陈刚 on 2017/10/10.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "CarBuyingDetailsViewController.h"
#import "RentalDetailsTableViewCell.h"

@interface CarBuyingDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *rentalArray;

@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) NSDictionary *payTypeDic;

@property (nonatomic, strong) NSDictionary *backPayTypeDic;

@property (strong, nonatomic) IBOutlet UIView *sectionHeardView;

@property (weak, nonatomic) IBOutlet UITableView *buyCarTable;

@property (weak, nonatomic) IBOutlet UILabel *orderStatusLabel;

@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

- (IBAction)cancelEvent:(UIButton *)sender;

@end

@implementation CarBuyingDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    
    [self loadData];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)setupView{
    [self showNav:@"订单详情"];
    [self.buyCarTable registerNib:[UINib nibWithNibName:@"RentalDetailsTableViewCell" bundle:[NSBundle mainBundle]] forHeaderFooterViewReuseIdentifier:@"Cell"];
    [self.cancelButton.layer setMasksToBounds:YES];
    [self.cancelButton.layer setCornerRadius:5];
    [self.cancelButton.layer setBorderWidth:1];
    [self.cancelButton.layer setBorderColor:HexColorInt32_t(FD1142).CGColor];
    self.payTypeDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"已接单",@"3",@"等接单",@"2",@"完成",@"5",@"评价",@"4",@"取消订单确认中",@"6",@"已取消",@"7", nil];
    self.backPayTypeDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"微信",@"2",@"支付宝",@"3",@"银联",@"4",@"余额",@"5", nil];
    
}

- (void)loadData{
    
    NSString *ordertype = [self.detailsDic objectForKey:@"typeid"];
    if ([ordertype integerValue] == 11) {
        self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订  单  号",@"下单时间",@"商家名称",@"车型名称", nil];
    }
    
    if ([ordertype integerValue] == 12 ||[ordertype integerValue] == 13) {
        self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订  单  号",@"下单时间",@"支付金额",@"支付方式",@"用途", nil];
    }
    
    if ([ordertype integerValue] == 7) {
        self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订  单  号",@"下单时间",@"支付金额",@"支付方式",@"收货地址",@"备注", nil];
    }
    
    if ([ordertype integerValue] == 8) {
        self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订  单  号",@"下单时间",@"支付金额",@"支付方式",@"开锁地址",@"备注", nil];
    }
    
    if ([ordertype integerValue] == 4) {
        self.titleArray = [[NSMutableArray alloc]initWithObjects:@"订  单  号",@"下单时间",@"支付金额",@"商家名称",@"收货地址",@"备注", nil];
    }
    
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSString *orderid = [self.detailsDic objectForKey:@"id"];
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:token forKey:@"verifyToken"];
    [parmas setValue:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setValue:orderid forKey:@"orderid"];
    [parmas setValue:ordertype forKey:@"orderTypeId"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/UserOrder/userOrderDetails",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"用户查看订单列表res:%@",responseObject);
        NSInteger state = [[responseObject objectForKey:@"status"] integerValue];
        NSString *msg = [responseObject objectForKey:@"msg"];
        NSDictionary *dataDic = [responseObject objectForKey:@"conetnt"];
        if ([ordertype integerValue] == 12 ||[ordertype integerValue] == 13 ||[ordertype integerValue] == 7 ||[ordertype integerValue] == 8 || [ordertype integerValue] == 4){
            dataDic = [responseObject objectForKey:@"content"];
        }
        if (state  == 200 && [dataDic isKindOfClass:[NSDictionary class]]) {
            [SVProgressHUD dismiss];
            NSString *orderdenumber = [dataDic objectForKey:@"ordernumber"];
            NSString *addtime = [self getyYearsTimeFormat:[dataDic objectForKey:@"addtime"]];
            NSString *merchantName = [dataDic objectForKey:@"merchantName"];
            NSString *ordername = [dataDic objectForKey:@"ordername"];
            NSString *orderstate = [dataDic objectForKey:@"orderstate"];
            NSString *price = [dataDic objectForKey:@"ordermoney"];
            if ([orderstate integerValue] == 5) {
                [self.cancelButton setHidden:NO];
            }
            [self.orderStatusLabel setText:[self.payTypeDic objectForKey:orderstate]];
            if ([ordertype integerValue] == 11) {
                self.rentalArray = [[NSMutableArray alloc]initWithObjects:orderdenumber,addtime,merchantName,ordername, nil];
            }
            
            if ([ordertype integerValue] == 12 || [ordertype integerValue] == 13) {
                NSString *payTypeString = [dataDic objectForKey:@"payment"];
                NSString *pplayType = [self.backPayTypeDic objectForKey:payTypeString];
                self.rentalArray = [[NSMutableArray alloc]initWithObjects:orderdenumber,addtime,price,pplayType,ordername, nil];
            }
            
            if ([ordertype integerValue] == 7 || [ordertype integerValue] == 8) {
                NSString *payTypeString = [dataDic objectForKey:@"payment"];
                NSString *pplayType = [self.backPayTypeDic objectForKey:payTypeString];
                NSString *addressString = [NSString stringWithFormat:@"%@ %@ \n %@",[dataDic objectForKey:@"consignee"],[dataDic objectForKey:@"phone"],[dataDic objectForKey:@"address"]];
                NSString *remaek = [dataDic objectForKey:@"remaek"];
                self.rentalArray = [[NSMutableArray alloc]initWithObjects:orderdenumber,addtime,price,pplayType,addressString,remaek, nil];
            }
            
            if ([ordertype integerValue] == 4) {
                NSString *addressString = [NSString stringWithFormat:@"%@ %@ \n %@",[dataDic objectForKey:@"goname"],[dataDic objectForKey:@"gophone"],[dataDic objectForKey:@"goaddress"]];
                NSString *remaek = [dataDic objectForKey:@"remarks"];
                self.rentalArray = [[NSMutableArray alloc]initWithObjects:orderdenumber,addtime,price,ordername,addressString,remaek, nil];
            }
            
            [self.buyCarTable reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:msg];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ordertype = [self.detailsDic objectForKey:@"typeid"];
    if ([ordertype integerValue] == 7 || [ordertype integerValue] == 8 || [ordertype integerValue] == 4) {
        if (indexPath.row == 4) {
            return 88;
        }else{
           return 44;
        }
    }else{
        return 44;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.rentalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *Identitfer  = @"Cell";
    RentalDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identitfer];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RentalDetailsTableViewCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSInteger typeidIndex = [[self.detailsDic objectForKey:@"typeid"] integerValue];
    if (typeidIndex == 11) {
        if (indexPath.row == self.rentalArray.count - 1) {
            [cell.valueLabel setTextColor:HexColorInt32_t(0F81FF)];
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }else if(indexPath.row == self.rentalArray.count - 2){
            [cell.valueLabel setTextColor:HexColorInt32_t(0F81FF)];
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }else{
            [cell setAccessoryType:UITableViewCellAccessoryNone];
            [cell.valueLabel setTextColor:HexColorInt32_t(000000)];
        }
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        [cell.valueLabel setTextColor:HexColorInt32_t(000000)];
    }
    
    [cell.itemLabel setText:[self.titleArray objectAtIndex:indexPath.row]];
    [cell.valueLabel setText:[self.rentalArray objectAtIndex:indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.rentalArray.count - 1) {
        NSLog(@"indexPath.row === %zd",indexPath.row);
    }
}

- (NSMutableArray *)rentalArray{
    if (!_rentalArray) {
        _rentalArray = [[NSMutableArray alloc]init];
    }
    return _rentalArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)cancelEvent:(UIButton *)sender {
    NSInteger typeidIndex = [[self.detailsDic objectForKey:@"typeid"] integerValue];
    NSString *orderId = [self.detailsDic objectForKey:@"id"];
    //洗车
    if (typeidIndex == 12) {
        [self coolyCancelOrder:orderId urlString:@"App/UserOrder/cancelCarWashingOrder"];
    }
}


- (void)coolyCancelOrder:(NSString *)orderId urlString:(NSString *)urlString{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:token forKey:@"verifyToken"];
    [parmas setValue:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setValue:orderId forKey:@"orderid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/%@",urlHead,urlString];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"取消苦力订单res:%@",responseObject);
        NSInteger state = [[responseObject objectForKey:@"status"] integerValue];
        NSString *msg = [responseObject objectForKey:@"msg"];
        if (state  == 200 ) {
            [SVProgressHUD showSuccessWithStatus:msg];
        }else{
            [SVProgressHUD showErrorWithStatus:msg];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

@end
